package com.douziit.meilianoa.controller;


import com.douziit.meilianoa.base.annotation.NoCheckToken;
import com.douziit.meilianoa.base.enums.ResponseCode;
import com.douziit.meilianoa.base.exception.TipException;
import com.douziit.meilianoa.base.request.PageRequest;
import com.douziit.meilianoa.base.response.ResponseData;
import com.douziit.meilianoa.base.response.ResponseUtil;
import com.douziit.meilianoa.base.utils.file.OSSPrivateStorage;
import com.douziit.meilianoa.base.utils.file.UploadDocumentUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共类控制器类
 */
@Slf4j
@RestController
@RequestMapping("/commer")
public class CommerController {

    @Autowired
    private OSSPrivateStorage ossPrivateStorage;

    @Value("${upload.file.path}")
    String path;

    @Value("${nginx.pre}")
    String nginxPre;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public ResponseData uploadFile(MultipartFile file) {
        String fileid = "";
        InputStream coverImgInputStream = null;
        if (file != null && !file.isEmpty()) {
            try {
                String fileName = file.getOriginalFilename();
                if (StringUtils.isBlank(fileName)) {
                    return ResponseUtil.jsonFail(ResponseCode.EMPTY_FILE);
                }
                coverImgInputStream = file.getInputStream();
                fileid = "file/" + System.currentTimeMillis()+ fileName.substring(fileName.lastIndexOf("."));
                log.info("查看保存之前fileId:"+fileid);
                fileid = ossPrivateStorage.saveFile(coverImgInputStream, fileid);
                log.info("上传文件成功：" + fileid);
                return ResponseUtil.jsonSuccess(fileid);
            } catch (Exception e) {
                log.error("文件上传失败" + e.getMessage());
                return ResponseUtil.jsonFail(ResponseCode.FILE_ERROR);
            }
        }
        return ResponseUtil.jsonFail(ResponseCode.EMPTY_FILE);
    }

    /**
     * 下载
     *
     * @param response
     * @param fileName
     * @return
     */
    @GetMapping("/downloadFile")
    @NoCheckToken
    public ResponseData downloadFile(HttpServletResponse response, String fileName) {
        if (StringUtils.isBlank(fileName)) {
            throw new TipException("文件名不能为空!");
        }
        try {
            ossPrivateStorage.download(response,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new TipException("文件下载出现异常");
        }
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     * 上传文件
     * @param fileid 压缩后的图片地址
     */
    @PostMapping("/uploadOriginal")
    @NoCheckToken
    public ResponseData uploadOriginalFile(
            @RequestParam("fileid") String fileid,
            MultipartFile file) {
        InputStream coverImgInputStream = null;
        if (file != null && !file.isEmpty()) {
            try {
                String fileName = file.getOriginalFilename();
                if (StringUtils.isBlank(fileName)) {
                    return ResponseUtil.jsonFail(ResponseCode.EMPTY_FILE);
                }
                coverImgInputStream = file.getInputStream();
                fileid = ossPrivateStorage.saveOriginalFile(coverImgInputStream, fileid);
                log.info("上传文件成功：" + fileid);
                return ResponseUtil.jsonSuccess(fileid);
            } catch (Exception e) {
                log.error("文件上传失败" + e.getMessage());
                return ResponseUtil.jsonFail(ResponseCode.FILE_ERROR);
            }
        }
        return ResponseUtil.jsonFail(ResponseCode.EMPTY_FILE);
    }


    /**
     * 文件上传
     *
     * @param files
     * @return 网络访问路径
     */
    @NoCheckToken
    @PostMapping("/uploadFile")
    public ResponseData uploadFile(@RequestParam(value = "files", required = false) MultipartFile[] files) {
        String idCardBackUrl;
        try {
            idCardBackUrl = UploadDocumentUtil.uploadFiles(path, files);
        } catch (IOException e) {
            throw  new TipException("上传失败");
        }
        idCardBackUrl=idCardBackUrl.replace(nginxPre,"");
        return ResponseUtil.jsonSuccess(idCardBackUrl);
    }

}
